package com.house.api.controller;

import com.house.common.exception.HouseException;
import com.house.common.result.HttpCodeEnum;
import com.house.common.result.Result;
import com.house.model.vo.HouseImageVo;
import com.house.service.service.IHouseImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
@Api(tags = "文件上传相关接口")
@Slf4j
public class UploadController {

    @Autowired
    private IHouseImageService houseImageService;

    @PostMapping("/headImage")
    @ApiOperation("上传首图")
    public String headImage(MultipartFile file,String houseId) {
        String url = houseImageService.uploadHeadImage(file, houseId);
       return url;
    }

    @PostMapping("/images")
    @ApiOperation("上传详细图片")
    public HouseImageVo images(MultipartFile file,String houseId){
        HouseImageVo houseImageVo = houseImageService.saveHouseImages(file, houseId);
        return houseImageVo;
    }

    private String getFilePath(String filename) {
        String suffix = filename.substring(filename.lastIndexOf("."));
        return UUID.randomUUID() + suffix;
    }
}
